package com.mxl.dao.channel.impl;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.util.Assert;

import com.mxl.dao.channel.DealRequestDao;
import com.mxl.dao.channel.model.DealRequest;
import com.mxl.datebase.orm.hibernate.BaseDaoSupport;
import com.mxl.datebase.orm.pagination.Page;

public class DealRequestDaoImpl extends BaseDaoSupport<DealRequest, Long>
		implements DealRequestDao {

	public DealRequest queryRequestBySeqNo(String orderId) {
		Assert.notNull(orderId, "query DealRequest orderId must be not null");
		return super.findUniqueBy("seqNo", orderId);
	}

	public void updateRequest(DealRequest dto) {
		super.update(dto);
	}

	public DealRequest saveReq(DealRequest dto) {
		return super.save(dto);
	}

	/**
	 * 查询115状态的订单到渠道获取结果
	 * 
	 * @return
	 */
	public List<DealRequest> queryNoResultOrder() {
		Page p = new Page();
		p.setPageSize(10);
		return this
				.find(p,
						Restrictions.eq("status", 115),
						Restrictions.le("queryTimes", 10),
						Restrictions
								.sqlRestriction("creationDate>=DATE_SUB(now(), INTERVAL 1 hour"));
	}

	/**
	 * 查询提交渠道失败的订单 101 999 的订单 一分钟跑一次
	 * 
	 * @return
	 */
	public List<DealRequest> querySubmitFailOrder() {
		Page p = new Page();
		p.setPageSize(10);
		Object[] status = { 101, 999 };
		return this
				.find(p,
						Restrictions.in("status", status),
						Restrictions.le("reissueTimes", 5),
						Restrictions
								.sqlRestriction("creationDate>=DATE_SUB(now(), INTERVAL 1 hour"),
						Restrictions
								.sqlRestriction("creationDate<=DATE_SUB(now(), INTERVAL 1 minute)"));
	}
}
